//===============================================================================================
//
// (c) Copyright Microsoft Corporation.
// This source is subject to the Microsoft Permissive License.
// See http://www.microsoft.com/resources/sharedsource/licensingbasics/sharedsourcelicenses.mspx.
// All other rights reserved.
//
//===============================================================================================

using System;
using System.Web.UI;
using System.IO;
using System.Threading;
using System.Globalization;
using System.Web.UI.HtmlControls;
using System.Web;
using System.Web.Security;

namespace MyWebPagesStarterKit.Controls
{
    public abstract class PageBaseClass : Page
    {
        protected WebSite _website;

        public PageBaseClass(){}

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
        }

        protected override void OnPreInit(EventArgs e)
        {
            base.OnPreInit(e);

            if (!Page.User.Identity.IsAuthenticated)
                Page.EnableViewState = false;
           
            _website = WebSite.GetInstance();

            if((_website.Theme == string.Empty) || (!Directory.Exists(Server.MapPath(string.Format("~/App_Themes/{0}", _website.Theme))))){
                string appThemesPath = Server.MapPath("~/App_Themes");
                string[] themes = Directory.GetDirectories(appThemesPath);
                if (themes.Length > 0)
                {
                    //if theme Mwpsk, select as default, else choose first theme
                    bool defaultTheme = false;
                    foreach (string theme in themes)
                    {
                        if (Path.GetFileName(theme) == "Mwpsk")
                            defaultTheme = true;
                    }
                    _website.Theme = defaultTheme == true ? "Mwpsk" : Path.GetFileName(themes[0]);
                }
                else
                {
                    _website.Theme = string.Empty;
                }
                _website.SaveData();
            }
            Page.Theme = _website.Theme;

            WebPage page = WebPage.Current;

            if(!string.IsNullOrEmpty(page.Theme))
                Page.Theme = page.Theme;

            //Created to test different themes at runtime, will be removed before publish.
            if (Page.Request.QueryString["theme"] != null)
                Session["theme"] = Page.Request.QueryString["theme"];
            if(Session["theme"]!=null)
                    Page.Theme = Convert.ToString(Session["theme"]);
 
            if(Request.RawUrl.ToLower().Contains("administration/"))
                Page.Theme = "Admin";

            if (!Request.RawUrl.ToLower().Contains("administration/"))
            {
                TemplatedContent content = (TemplatedContent)Page.Master.FindControl("content");
                content.SkinID = WebPage.Current.TemplateName;
            }
        }

        protected override void InitializeCulture()
        {
            // Initialize Resource Manager - with no effect if it's already been initialized
            string LocaleID = WebSite.GetInstance().LocaleID;
            UICulture = LocaleID;
            Culture = LocaleID;
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(LocaleID);
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(LocaleID);
            
            //due to an error of freetextbox, all the cultures must use a dot as NumberDecimalSeparator
            Thread.CurrentThread.CurrentCulture.NumberFormat.NumberDecimalSeparator = ".";

            base.InitializeCulture();
        }

        protected void Page_PreRender(object sender, EventArgs e)
        {
            if (_website.Theme == "Arabic")
            {
                HtmlGenericControl body = (HtmlGenericControl)Master.FindControl("body1");
                body.Attributes.Add("dir", "rtl");
            }
        }
    }
}
